import {createRouter, createWebHistory} from 'vue-router'
import HomeView1 from '@/views/HomeView1.vue'
import HomeView2 from '@/views/HomeView2.vue'
import HomeView3 from '@/views/HomeView3.vue'
import IndexView1 from '@/views/IndexView1.vue'
import IndexView2 from '@/views/IndexView2.vue'
import IndexView3 from '@/views/IndexView3.vue'
import OwnerView from '@/views/OwnerView.vue'
import DepartmenView from '@/views/DepartmenView.vue'
import RepairPersonnelView from '@/views/RepairPersonnelView.vue'
import LoginView from '@/views/LoginView.vue'
import RepairView1 from '@/views/RepairView1.vue'
import RepairView2 from '@/views/RepairView2.vue'
import RepairView3 from '@/views/RepairView3.vue'
import loginApi from "@/api/loginApi.js";
import {useTokenStore} from "@/stores/token.js";


const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [{
        path: '/login',
        name: 'login',
        component: LoginView
    }, {
        path: '/home2',
        name: 'home2',
        component: HomeView2,
        redirect: '/home2/index2',
        children: [{
            path: '/home2/repair2',
            component: RepairView2
        }, {
            path: '/home2/index2',
            component: IndexView2
        }, {
            path: '/home2',
            component: HomeView2
        }
        ]
    }, {
        path: '/home3',
        name: 'home3',
        component: HomeView3,
        redirect: '/home3/index3',
        children: [{
            path: '/home3/repair3',
            component: RepairView3
        }, {
            path: '/home3/index3',
            component: IndexView3
        }, {
            path: '/home3/home3',
            component: HomeView3
        }
        ]
    }, {
        path: '/home1',
        name: 'home1',
        component: HomeView1,
        redirect: '/index1',
        children: [
            {
                path: '/repair1',
                component: RepairView1
            }, {
                path: '/dept',
                component: DepartmenView
            }, {
                path: '/index1',
                component: IndexView1
            }, {
                path: '/owner',
                component: OwnerView
            }, {
                path: '/repairPersonnel',
                component: RepairPersonnelView
            }, {
                path: '/home1',
                component: HomeView1
            }
        ]
    },
        {
            path: '/about',
            name: 'about',
            component: () => import('../views/AboutView.vue')
        }
    ]
})
//导航守卫
router.beforeEach((to, from) => {
    if (to.path == '/login') {
        return true;
    } else {
        const tokenStore = useTokenStore()
        //判断Store中是否有token
        if (!tokenStore.tokenStr) {
            return "/login";
        } else {
            loginApi.select().then(resp => {
                if (resp.data.root == 2) {
                    const home2Regex = /^\/home2\/.*/
                    if (home2Regex.test(to.path)) {
                        console.log(resp.data.root)
                        return true;
                    } else {
                        router.push("/login");
                    }
                } else if (resp.data.root== 3) {
                    const home3Regex = /^\/home3\/.*/
                    if (home3Regex.test(to.path)) {
                        return true;
                    } else {
                       router.push("/login") ;
                    }
                }else{
                    return true
                }
            })
        }
    }

});
export default router
